//
//  ExpViewController.m
//  jiyujiaProgram
//
//  Created by Jame on 15/12/15.
//  Copyright © 2015年 中良 赵. All rights reserved.
//

#import "ExpViewController.h"
#import "AddExpViewController.h"
#import "ExpModel.h"
#import "ExpCell.h"

@interface ExpViewController () <UITableViewDataSource,UITableViewDelegate>

@end

@implementation ExpViewController
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"教学经历"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];
    if (self.isSelf) {
        [self creatNavigationBarRightItemWithRightTitle:@"添加" RightImage:nil];
    }
    [self creatTableView];
}


- (void)viewWillAppear:(BOOL)animated
{
    [self creatHttpRequest];
}


- (void)creatHttpRequest
{
    NSDictionary *param = @{
                            @"teacher_Id":self.memberId,
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(EXPINFO) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [ExpModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"ExpData",
                         };
            }];
            
            if (_dataArray) {
                [_dataArray removeAllObjects];
            }else{
                _dataArray = [[NSMutableArray alloc] init];
            }
            
            ExpModel *exp = [[ExpModel alloc] init];
            exp = [ExpModel objectWithKeyValues:result];
            [_dataArray addObjectsFromArray:exp.data];
            [_tableView reloadData];
        }
    }];

}

- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBtnClick:(id)rightSender
{
    AddExpViewController *vc = [[AddExpViewController alloc] init];
    vc.memberId = self.memberId;
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)creatTableView
{
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, ECSCREEN_H - 64) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    _tableView.tableFooterView = [[UIView alloc]init];
    
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}


#pragma mark - tableViewNumberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}


#pragma mark - tableViewHeightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExpData *data = _dataArray[indexPath.row];
    CGSize labelSize = STRING_SIZE_FONT_HEIGHT(ECSCREEN_W - 16, data.descr, 13.f);
    NSLog(@"*****HEIGHT******%f",labelSize.height);
    return 40 + labelSize.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdentifier = @"expCell";
    ExpCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (nil == cell) {
        NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"ExpCell" owner:nil options:nil];
        for (NSObject *obj in arr) {
            if ([obj isKindOfClass:[ExpCell class]]) {
                cell = (ExpCell *)obj;
                if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                    [cell setSeparatorInset:UIEdgeInsetsZero];
                }
                if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                    [cell setLayoutMargins:UIEdgeInsetsZero];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                break;
            }
        }
    }
    [cell setCellWithModel:_dataArray WithIndexPath:indexPath];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
